package com.taokatao.cardmee.business.carddetail;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.WebActivity;
import com.taokatao.cardmee.api.model.CardDetailInfo;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CardDetailBannerLayout extends LinearLayout implements View.OnClickListener {
    @BindView(R.id.t_banner_title) TextView tBannerTitle;
    @BindView(R.id.t_banner_desc) TextView tBannerDesc;
    @BindView(R.id.i_banner_url) ImageView iBannerUrl;

    @BindView(R.id.l_content) View lContent;

    private CardDetailInfo.Banner mBanner;

    public CardDetailBannerLayout(Context context) {
        this(context, null);
    }

    public CardDetailBannerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        ButterKnife.bind(this, LayoutInflater.from(context).inflate(R.layout.layout_card_detail_banner, this));
    }

    public void setData(CardDetailInfo.Banner banner) {
        if (banner == null) return;
        mBanner = banner;
        tBannerTitle.setText(mBanner.bannerName);
        tBannerDesc.setText(mBanner.bannerDec);
        if (TextUtils.isEmpty(mBanner.bannerUrl)) {
            iBannerUrl.setVisibility(View.GONE);
            lContent.setOnClickListener(null);
        } else {
            iBannerUrl.setVisibility(View.VISIBLE);
            lContent.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        if (v == lContent) {
            if (mBanner != null && !TextUtils.isEmpty(mBanner.bannerUrl)) {
                Utils.onMobEvent(getContext(), "clickLinkOfDetail");
                Intent intent = new Intent(getContext(), WebActivity.class);
                intent.putExtra("extra_url", mBanner.bannerUrl);
                getContext().startActivity(intent);
            }
        }
    }
}
